package com.yanqu.road.server.gameplayer.module.rebate;


import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.log.LogVipExp;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.rebate.UserRebateInfo;
import com.yanqu.road.logic.bussiness.player.UserRebateBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.RechargeModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.rebate.RebateMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

public class RebateModule extends GeneralModule {

    public RebateModule(GamePlayer player) {
        super(player);
    }

    private UserRebateInfo userRebateInfo;

    public synchronized void rebate2User(){

        if (userRebateInfo != null && !userRebateInfo.isGet()) {
            // 还未发送邮件 创建奖励邮件
            UserInfo userInfo = player.getUserInfo();

            long IngotsNum = userRebateInfo.getRecharge() * 4;

            long vipExp = userRebateInfo.getVipExp();

            Property reward = new Property();

            reward.addProperty(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(IngotsNum));

            userRebateInfo.setGet(true);
            userRebateInfo.setGetTime(System.currentTimeMillis());
            userRebateInfo.setServerId(GameServer.getInstance().getServerId());
            userRebateInfo.setUserId(userInfo.getUserId());
            userRebateInfo.setNickName(userInfo.getNickName());


            long originalValue = player.getUserInfo().getVipExp();
            player.getModule(RechargeModule.class).addVipExp(vipExp);

            AutoLogMgr.add(new LogVipExp(player.getUserId(), originalValue, vipExp, originalValue + vipExp, 2, "返利虚拟充值"));//返利虚拟充值

            String title = MultipleLanguageMgr.getContent(MailManager.REBATE_TITLE, userInfo.getLanguage());
            String content = MultipleLanguageMgr.getContent(MailManager.REBATE_CONTENT, userInfo.getLanguage());
            MailManager.sendMail(userInfo.getUserId(), eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), content, title);
            log.error("发送充值返利邮件成功，userId: {}，昵称: {}，vip : {},充值：{}，返还：{}",
                    userInfo.getUserId(), userInfo.getNickName(), vipExp, userRebateInfo.getRecharge(), IngotsNum);
        }
    }

    @Override
    public boolean loadData() {
        UserInfo userInfo = player.getUserInfo();
        userRebateInfo = RebateMgr.getRebateInfo(userInfo.getPlatformUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    //TODO server
    @Override
    public boolean saveData() {
        if(userRebateInfo != null && userRebateInfo.getOp() == DataStatus.Update){
            UserRebateBussiness.updateUserRebateInfo(userRebateInfo);
        }
        return true;
    }
}
